#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
	char *path="/home/abby/bin/source/test.txt";
	int file;
	char buf[28] ,buf2[]="welcomed to openSUSE!";
	file=open(path,O_RDWR);
	if(file==-1)
	{
		perror("open file failed !\n");
		return 1;
	}
	else
		printf("\nopen file sucessful!\n");
	if((read(file,buf,10))==-1)  /* read 10 consoles form 'file' to 'buf'*/
	{
		perror("read file content failed !\n");
		return 1;
	}
	else
	{
		printf("\nread file contents sucessful !\n");
	//	printf("\nthe contents are :%s",buf);   /*为什么这行注释掉了和没注释掉效果一样？*/
	}
	if((lseek(file,8,SEEK_SET))<0) /* start from the end of 'file' */
	{
		perror("lseek file failed !\n");
		return 1;
	}
	else
	{
		if((write(file,buf2,sizeof(buf2)))<0) /* write the contents of	 'file' into 'buf2' */
		{
			printf("\nwrite file failed !\n");
			return 1;
		}
		else
		{
			printf("\n\n");
			printf("\nwrite file sucessful !\n");/*现在，file里卖的内容是'you are welcome to openSUSE'*/ 
		}
	}
	close(file);
	if((file=open(path,O_RDWR))<0)
	{
		perror("try to open file failed \n\a");
		return 1;
	}
	if((read(file,buf,28))<0)
	{
		perror("try to read file failed \n\a");
		return 1;
	}
	else
	{
		printf("\nthe datas contained in the file are:\t%s",buf); /*把内容已经变为you are wle××的file读到buf中再把buf打印出来*/
	}
	if(close(file)<0)
	{
		perror("close file failed !!!\n\a");
		return 1;
	}
	else 
	{
		puts("\n");
		printf("\nplease gvie the answer to the following equation\n");
		int a ,b;
		b=1+1;
		printf("\t1+1=?\n");
		scanf("%d",&a);
		if(a==b)
		{
			printf("\n You Are Right , See You !\n");
			puts("\n");
		}
		else
		{
			printf(" You Are So Stupid , How Can You Get This File?\n");
			puts("\a");
		}
	}
	return 0;
}
